home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / SORTING.SWG / 0009_Elevator Sort.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  76 lines

  1. {
  2. >   Thanks For the code...   It worked great!  BTW, why are there so many
  3. >   different sorting methods?  Quick, bubble, Radix.. etc, etc
  4.  
  5. Yes, there are lots of sorting algorithms out there! I also found this out
  6. the hard way! :-) A couple of years ago, I only knew the so-called "bubble"
  7. sort, and decided to create my own sorting algorithm. It would have to be
  8. faster than bubble, yet remaining small, simple, and not memory hungry.
  9. and I did it, but only to find out a few weeks later that there were much
  10. better sorts than the one I created... But it sure was great fun beating
  11. bubble! (which is brain-dead anyway! ;-)
  12.  
  13. So here it is, my two cents to the history of sorting algorithms, the
  14. amazing, blazingly fast (*)... ELEVAtoR SorT!... Why ELEVAtoR??, you ask in
  15. unison! Because it keeps going up & down! :-)
  16. }
  17.  
  18. Program mysort;
  19.  
  20. Uses Crt;
  21.  
  22. Const
  23.   max = 1000;
  24.  
  25. Type
  26.   list = Array[1..max] of Word;
  27.  
  28. Var
  29.   data  : list;
  30.   dummy : Word;
  31.  
  32.  
  33. Procedure elevatorsort(Var a: list; hi: Word);
  34.  
  35. Var
  36.   lo,
  37.   peak,
  38.   temp,
  39.   temp2 : Word;
  40.  
  41. begin
  42.   peak := 1;
  43.   lo   := 1;
  44.   Repeat
  45.     temp  := a[lo];
  46.     temp2 := a[lo + 1];
  47.     if temp > temp2 then
  48.     begin
  49.       a[lo]     := temp2;
  50.       a[lo + 1] := temp;
  51.       if lo <> 1 then dec(lo);
  52.     end
  53.       else
  54.     begin
  55.       inc(peak);
  56.       lo:=peak;
  57.     end;
  58.   Until lo = hi;
  59. end;
  60.  
  61.  
  62. begin
  63.   ClrScr;
  64.   Writeln('Generating ', max ,' random numbers...');
  65.   randomize;
  66.   For dummy:=1 to max do data[dummy]:=random(65535);
  67.   Writeln('Sorting random numbers...');
  68.   elevatorsort(data,max);
  69.   For dummy:=1 to max do Write(data[dummy]:5,'   ');
  70. end.
  71.  
  72. {
  73. (*) it's speed lies somewhere between "BUBBLE" and "inSERT"; it's much
  74. faster than "BUBBLE", and a little slower than "inSERT"... :-)
  75. }
  76.